<?php
use Phalcon\Mvc\View;
class PortalController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle(" 门户");
        parent::initialize();
    }

    public function indexAction()
    {
	    require APP_PATH . 'app/cas/cas.php';

		//  CAS Server 主机
		$cas_host = '192.168.0.38';
		 
		// CAS Server 路径
		$cas_context = '/cas-server-webapp';
		 
		// CAS server.端口
		$cas_port = 8080;
		 
		// <span>CAS Server </span>证书
		//$cas_server_ca_cert_path = './ssoserver.cer';
		
		// Enable debugging
		phpCAS::setDebug('debug.log');
		 
		 
		// Initialize phpCAS
		phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
		 
		 
		// For production use set the CA certificate that is the issuer of the cert
		// on the CAS server and uncomment the line below
		// phpCAS::setCasServerCACert($cas_server_ca_cert_path);
		 
		 
		// For quick testing you can disable SSL validation of the CAS server.
		// THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION.
		// VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL!
		phpCAS::setNoCasServerValidation();
		 
		 
		// force CAS authentication
		phpCAS::forceAuthentication();

		// 取得登陆用户id
		$casUserId = phpCAS::getUser('id');

        $header = new PortalController();
        $header->headerAction($casUserId);
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
    }

    public function headerAction($casUserId)
    {
        $serverUsers = new ServerUsers();
        $serverUser = $serverUsers->getServerUserByCasUser($casUserId);
        $businessSystems = $serverUsers->getBusinessSystemsByCasUser($casUserId);
        $this->view->setVars(array("businessSystems" => $businessSystems, "userId" => $serverUser->id, "name" => $serverUser->name));
    }

    public function  setUpAction($id)
    {
        if (!$this->request->isPost())
        {
            $userId = ServerUsers::findFirstById($id);
            if (!$userId) {
                $this->flash->error("密码修改失败！");
                //return $this->forward("portal/index");
            }
            $this->view->form = new PortalForm($userId, array('edit' => true));
        }
        $this->view->setVars(array("userId" => $id,"name" => $userId->name));
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
    }


    public function saveAction()
    {
        if(!$this->request->isPost()){
            $this->flash->error("提交方式不是POST");
            return $this->forward('portal/index');
        }

        $id = $this->request->getPost("id", "int");
        $serverUser = ServerUsers::findFirstById($id);
        if(!$serverUser)
        {
            $this->flash->error("业务用户没有信息数据！");
            return $this->forward("portal/index");
        }

        $form = new PortalForm();
        $this->view->form = $form;
        $data = $this->request->getPost();
        if (!$form->isValid($data, $serverUser))
        {
            foreach ($form->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('portal/edit/' . $id);
        }

        if ($serverUser->save() == false)
        {
            foreach ($serverUser->getMessages() as $message)
            {
                $this->flash->error($message);
            }
            return $this->forward('portal/edit/' . $id);
        }
        $form->clear();
        $this->flash->success("用户保存成功！");
        return $this->forward("portal/index");
    }

    public function editAction($id)
    {
        if (!$this->request->isPost())
        {
            $userId = ServerUsers::findFirstById($id);
            if (!$userId) {
                $this->flash->error("业务用户修改失败！");
                return $this->forward("portal/index");
            }
            $this->view->form = new PortalForm($userId, array('edit' => true));
        }
    }

}
